package FSMDemo.entity.Harvester.states
{
	import FSMDemo.entity.Building;
	import FSMDemo.entity.Harvester.Harvester;
	import FSMDemo.entity.mine.Mine;
	
	import com.cheezeworld.AI.AISettings;
	import com.cheezeworld.AI.Behaviors.Arrive;
	import com.cheezeworld.AI.FSM.IState;
	import com.cheezeworld.entity.Entity;
	
	import flash.events.EventDispatcher;

	public class WorkState extends EventDispatcher implements IState
	{
		public function setEntity( a_entity:* ) : void
		{
			if( !(a_entity is Harvester) )
			{
				throw new Error( "<WorkState> a_entity must be of type Harvester!" );
			}
			m_entity = a_entity;
		}
		
		public function getEntity() : *
		{
			return m_entity;
		}
		
		public function WorkState()
		{
			
		}
		
		public function enter():void
		{
			var gameEntities:Array = m_entity.parent.getChildren();
			for each( var entity:Entity in gameEntities )
			{
				if( entity is Mine )
				{
					m_mine = entity as Mine;										
				}
				
				if( entity is Building )
				{
					m_base = entity as Building;
				}
			}
		}
		
		public function execute():void
		{
			if( m_entity.goldOnHand >= m_entity.capacity )
			{
				if( m_entity.actualPos.distanceTo( m_base.actualPos ) > m_base.radius + m_entity.radius )
				{
					m_entity.status = "Going to base to drop off gold."
					m_entity.FSM.changeState( new GoToLocationState( m_base.actualPos, m_base.radius ));	
				}
				else
				{
					m_entity.goldOnHand = 0;
					m_entity.status = "Going to the mine to pick up gold";
					m_entity.FSM.changeState( new GoToLocationState( m_mine.actualPos, m_mine.radius ));					
				}
			}
			else if( m_mine.remainingMinerals <= 0 )
			{
				if( m_entity.goldOnHand > 0 )
				{
					if( m_entity.actualPos.distanceTo( m_base.actualPos ) > m_base.radius + m_entity.radius )
					{
						m_entity.status = "Going to base to drop off gold."
						m_entity.FSM.changeState( new GoToLocationState( m_base.actualPos, m_base.radius ));	
					}
					else
					{
						m_entity.goldOnHand = 0;
					}						
				}
				else
				{
					m_entity.status = "Mine depleted...Nothing to do";
					m_entity.FSM.changeState( new WanderState() );
					return;
				}
					
			}
			else
			{
				if( m_entity.actualPos.distanceTo( m_mine.actualPos ) > m_mine.radius + m_entity.radius )
				{
					m_entity.status = "Going to the mine to pick up gold."
					m_entity.FSM.changeState( new GoToLocationState( m_mine.actualPos, m_mine.radius ));	
				}
				else
				{
					m_entity.status = "Arrived at mine, Harvesting...";
					m_entity.FSM.changeState( new MiningState( m_mine ));										
				}
			}
			
				
			
			if( m_mine.actualPos.distanceSqTo( m_entity.actualPos ) > 50 * 50 )
			{
				m_entity.steering.clearBehaviors();
				m_entity.steering.addBehavior( new Arrive( m_mine.actualPos, AISettings.arriveFast ));
			}
		}
		
		public function exit():void
		{
		}
		
		private var m_mine:Mine;
		private var m_base:Building;
		private var m_entity:Harvester;
		
	}
}